package com.stylefeng.guns.rest.user;

import com.alibaba.dubbo.config.annotation.Service;

import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.stylefeng.guns.core.util.MD5Util;
import com.stylefeng.guns.rest.common.persistence.dao.MoocUserTMapper;
import com.stylefeng.guns.rest.common.persistence.model.MoocUserT;
import com.stylefeng.guns.api.user.vo.UserInfoModel;
import com.stylefeng.guns.api.user.vo.UserModel;
import com.stylefeng.guns.api.user.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.transaction.annotation.Transactional;

import java.util.Date;

@Component
@Service(interfaceClass = UserService.class,loadbalance = "roundrobin")
public class UserServiceImpl implements UserService {

    @Autowired
    private MoocUserTMapper moocUserTMapper;

    @Override
    public int login(String username, String password) {
       MoocUserT moocUserT = new MoocUserT();
       moocUserT.setUserName(username);
       MoocUserT result = moocUserTMapper.selectOne(moocUserT);
       if(result != null){
           String md5Pass = MD5Util.encrypt(password);
           if(md5Pass.equals(result.getUserPwd())){
               return result.getUuid();
           }
       }
       return 0;
    }

    @Override
    @Transactional
    public boolean register(UserModel userModel) {
        MoocUserT moocUserT = new MoocUserT();
        moocUserT.setUserName(userModel.getUsername());
        moocUserT.setEmail(userModel.getEmail());
        moocUserT.setAddress(userModel.getAddress());
        moocUserT.setUserPhone(userModel.getPhone());

        String md5Pass = MD5Util.encrypt(userModel.getPassword());
        moocUserT.setUserPhone(md5Pass);

        Integer insert = moocUserTMapper.insert(moocUserT);
        if(insert > 0){
            return true;
        }else {
            return false;
        }
    }

    @Override
    public boolean checkUsername(String username) {
        EntityWrapper<MoocUserT> entityWrapper = new EntityWrapper<>();
        entityWrapper.eq("user_name",username);
        Integer count = moocUserTMapper.selectCount(entityWrapper);
        if(count != null && count > 0){
            return false;
        }else {
            return true;
        }

    }

    private UserInfoModel do2UserInfo(MoocUserT moocUserT){
        UserInfoModel model = new UserInfoModel();
        model.setHeadAddress(moocUserT.getHeadUrl());
        model.setPhone(moocUserT.getUserPhone());
        model.setUpdateTime(moocUserT.getUpdateTime().getTime());
        model.setEmail(moocUserT.getEmail());
        model.setUsername(moocUserT.getUserName());
        model.setNickname(moocUserT.getNickName());
        model.setLifeState(moocUserT.getLifeState()+"");
        model.setBirthday(moocUserT.getBirthday());
        model.setAddress(moocUserT.getAddress());
        model.setSex(moocUserT.getUserSex());
        model.setBeginTime(moocUserT.getBeginTime().getTime());
        model.setBiography(moocUserT.getBiography());
        return model;
    }

    @Override
    public UserInfoModel getUserInfo(int uuid) {

        MoocUserT moocUserT = moocUserTMapper.selectById(uuid);
        UserInfoModel userInfoModel = do2UserInfo(moocUserT);
        return userInfoModel;
    }

    @Override
    public UserInfoModel updateUserInfo(UserInfoModel model) {
       MoocUserT moocUserT = new MoocUserT();
       moocUserT.setUuid(model.getUuid());
       moocUserT.setNickName(model.getNickname());
       moocUserT.setLifeState(Integer.parseInt(model.getLifeState()));
       moocUserT.setBirthday(model.getBirthday());
       moocUserT.setBiography(model.getBiography());
       moocUserT.setBeginTime(time2Date(model.getBeginTime()));
       moocUserT.setHeadUrl(model.getHeadAddress());
       moocUserT.setEmail(model.getEmail());
        moocUserT.setAddress(model.getAddress());
        moocUserT.setUserPhone(model.getPhone());
        moocUserT.setUserSex(model.getSex());
        moocUserT.setUpdateTime(time2Date(System.currentTimeMillis()));

        Integer isSuccess = moocUserTMapper.updateById(moocUserT);
        if(isSuccess > 0){
            return getUserInfo(moocUserT.getUuid());
        }

        return null;
    }

    private Date time2Date(long beginTime) {
        Date date = new Date(beginTime);
        return date;
    }
}
